<?php

namespace app\controller;

use app\BaseController;
use app\model\Site;
use think\facade\Db;
use think\facade\Session;
use think\facade\Request;
use app\utils\Utils;
use mailer\facade\Mailer;
use app\model\Log;
use think\facade\Cookie;

class Api extends BaseController
{
    static public function email() // 邮件系统
    {
        // 摆烂（确信）
        $app_id = Request::post('appid', false);
        $key = Request::post('key', false);
        $title = Request::post('title', false);
        $to = Request::post('to', false);
        $text = Request::post('text', false);
        $name = Request::post('name', false);
        if (!$app_id && !$key && !$title && !$to && !$text && !$name) {
            $return = array('info' => "请检查请求参数，存在缺少", 'code' => 403);
        }

        $app = Db::table('site_api')->where('app_id', $app_id)->select()->toArray(); // 查询数据
        if (count($app) == 0) {
            $return = array('info' => "请检查appid", 'code' => 404);
        } else {
            if (hash('sha256', $title . $to . $name . $text . $app[0]['key']) == $key) { //校验成功
                if ($app[0]['times'] == 0) { //没次数
                    $return = array('info' => "调用次数不足", 'code' => 403);
                } else {
                    $smtp = include("../config/mailer.php");
                    Mailer::from($smtp['username'])
                        ->to($to)
                        ->subject($title)
                        ->text($text)
                        ->send();

                    // times 字段减 1
                    Db::table('site_api')
                        ->where('app_id', $app_id)
                        ->dec('times') //减1
                        ->update();

                    $return = array('code' => 200);
                }
            } else {
                $return = array('info' => "请检查key", 'code' => 403);
            }
        }
        return json_encode($return);
    }

    static public function get_banner() // 获取首页轮播数据
    {
        header('Content-Type: application/json');

        $result = Db::table('site_banner')->select()->toArray(); // 查询数据
        return Utils::api_response(200, "获取成功", $result);
    }

    static public function logout() // 退出登录
    {
        $email = Session::get('email', '');
        $pw = Session::get('password', '');
        $my_user_info = Utils::user($email, $pw);
        if ($my_user_info['id'] == '') {
            //未登录
            Session::clear();
            return json(array("code" => 403, 'msg' => '未登录'));
        }

        Db::table('site_token')->where('user_id', $my_user_info['id'])->delete();

        //过滤token的非法字符，只允许字母和数字
        $keyID = preg_replace('/[^a-zA-Z0-9]/', '', Cookie::get("PHPSESSID"));
        unlink("../runtime/session/sess_" . $keyID);
        Session::clear();

        return Utils::api_response(200, "退出成功");
    }

    static public function get_new_things() // 新产品系统
    {
        header('Content-Type: application/json');

        $result = Db::table('site_new_things')->select()->toArray(); // 查询数据
        return Utils::api_response(200, "获取成功", $result);
    }

    static public function get_activities() // 活动系统
    {
        header('Content-Type: application/json');

        $result = Db::table('site_activity')->select()->toArray(); // 查询数据
        return Utils::api_response(200, "获取成功", $result);
    }
}
